/**
  * The Window in which the game is displayed
  *
  * @author "Brian MacCarthy","Owen Grogan";
  * @created "2007-03-04 Brian MacCarthy";
  * @revised "$Date: 2007-03-06 19:11:00 $"; 
  */


package exfist;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameWindow extends Canvas implements KeyListener
{
	JFrame main_window;
	Intro intro;
	int introcount;
	Arena arena;
	Round r;
	boolean isIntroOn=true;
	private final int Height = 768;
	private final int Width = 1024;
	KeyBindings kb;
	Player playa;
	BufferStrategy bs;
	public static void main(String[] args)
	{
		GameWindow gw=new GameWindow();
		gw.play();

	}
	/**
	 * TODO: The intro blocks the arena from being drawn even after the enter key is pressed.
	 *
	 */
	private GameWindow()
	{
		playa = new Player();
		arena=new Arena();
		intro=new Intro();
		r=new Round();
		
	    try {
	        // From file
	        Sequence sequence = MidiSystem.getSequence(new File("imgs/explodingfist.mid"));
	    
	        // Create a sequencer for the sequence
	        Sequencer sequencer = MidiSystem.getSequencer();
	        sequencer.open();
	        sequencer.setSequence(sequence);
	    
	        // Start playing
	        sequencer.setLoopCount(2);
	        sequencer.start();
	    } catch (IOException e) {
	    } catch (MidiUnavailableException e) {
	    } catch (InvalidMidiDataException e) {
	    }
	    
		JFrame wayFrame=new JFrame("The Way of the Exploding Fist");
		JPanel wayPanel=(JPanel)wayFrame.getContentPane();
		setBounds(0, 0, Width, Height);
		wayPanel.setPreferredSize(new Dimension(Width, Height));
		wayPanel.setLayout(null);
		wayPanel.add(this);
		wayFrame.setBounds(50,50,Width,Height);
		wayFrame.setVisible(true);
		wayFrame.setResizable(false);
		wayFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		addKeyListener(this);
		createBufferStrategy(2);
		bs=getBufferStrategy();
	}
	
	public void paintIT(){
		Graphics2D g = (Graphics2D)bs.getDrawGraphics();
		if(isIntroOn){
			g.drawImage(intro.getCurrentImage(),0,0,this);
			g.dispose();
		}else{
			g.drawImage(arena.getArenaBackground(), 1024, 0, 0, 768, 0, 0, 1600, 1200, this);
			g.drawImage(playa.getCurrentSprite(),playa.getX(),playa.getY(),this);
		}
		r.paintStatus(g);
		bs.show();
	}
	public void play(){
		while(isVisible()){
			paintIT();
			if(!isIntroOn){
				r.time();
			}
		}
	}
	
	public void keyPressed(KeyEvent e) 
	{
		switch(e.getKeyCode())
		{
			//Pressing enter will start the game
			case KeyEvent.VK_ENTER : isIntroOn = false;	break;
			case KeyEvent.VK_RIGHT : playa.keyPressed(e);break;
			case KeyEvent.VK_LEFT: playa.keyPressed(e);break;
			case KeyEvent.VK_R: r.resetTime();break;
		}
		
	}
	public void keyReleased(KeyEvent e) 
	{
	}
	public void keyTyped(KeyEvent e) 
	{
	}
}